import React,{Component} from "react";
import {Form, Input, message, Modal} from "antd";
import JobService from "../service/JobService";

class JobAdd extends Component{
    layout = {
        labelCol: {span: 5},
        wrapperCol: {span: 16},
    };

    validateMessages = {
        required: "'${label}'不能为空!",
    };


    // 表单对象
    formRef = React.createRef();
    jobService = new JobService();

    constructor(props) {
        console.log("chiudl",props)
        super(props);
        this.state={
            visible:false,
            init:0
        }
    }


    /**
     * 组件初始化以及修改state 后会执行
     * @param props
     * @param state
     */
   /* static getDerivedStateFromProps(props,state){
        if (!state.init){
            state.visible=props.visible
            state.init = props.visible
        }else{
            state.init = 0
        }
        return null;
    }*/
    componentDidMount() {
        this.props.onRef(this);
    }

    showModal(){
        this.setState({
            visible:true
        })
    }

    refreshJobList(){
        this.props.refreshJobList()
    }

    /**
     * 确定添加定时任务
     * @param e
     */
    handleOk = e => {
        let formValues = this.formRef.current.getFieldsValue();
        this.jobService.addJob(formValues.job).then(resp => {
            const result = resp.data
            if (result && result.code===0){
                this.refreshJobList();
                message.success('添加成功');
            }
        }).catch(err=>{
            message.error("系统错误，请联系管理员")
        })
        this.setState({
            visible: false,
        });
    };


    /**
     * 取消添加定时任务
     * @param e
     */
    handleCancel = e => {
        console.log(e);
        this.setState({
            visible: false,
        });
        console.log(this.state)
    };


    render() {
        return <Modal
            title="添加任务"
            destroyOnClose={true}
            visible={this.state.visible}
            onOk={this.handleOk}
            onCancel={this.handleCancel}
            okText="提交"
            cancelText="取消"
        >
            <Form {...this.layout} ref={this.formRef}
                  validateMessages={this.validateMessages}>
                <Form.Item name={['job', 'jobName']} label="任务名称" rules={[{required: true}]}>
                    <Input/>
                </Form.Item>
                <Form.Item name={['job', 'jobClassName']} label="执行类名" rules={[{required: true}]}>
                    <Input/>
                </Form.Item>
                <Form.Item name={['job', 'jobGroupName']} label="分组名称" rules={[{required: true}]}>
                    <Input/>
                </Form.Item>
                <Form.Item name={['job', 'cronExpression']} label="cron表达式" rules={[{required: true}]}>
                    <Input/>
                </Form.Item>
                <Form.Item name={['job', 'description']} label="任务描述" rules={[{required: false}]}>
                    <Input/>
                </Form.Item>
            </Form>
        </Modal>

    }
}

export default JobAdd
